package com.uenpay.im.adapter;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView.ScaleType;

import com.aswife.ui.MaskImageView;
import com.uenpay.im.entities.RotateImageViewEntity;
import com.uenpay.im.weight.MyViewPage;

import java.util.ArrayList;

public class RotaryPhotoAdapter extends PagerAdapter {

	private ArrayList<RotateImageViewEntity> _list;
	private ArrayList<MaskImageView> _viewList;
	private MyViewPage.IRotaryPhotoListener callBackListener;
	private Context mContext;

	public RotaryPhotoAdapter(Context cx,MyViewPage.IRotaryPhotoListener _callBackListener) {
		mContext = cx;
		callBackListener = _callBackListener;
	}

	public void addData(ArrayList<RotateImageViewEntity> list) {
		this._list = list;
		if(_viewList == null) {
			_viewList = new ArrayList<MaskImageView>();
		} else {
			_viewList.clear();
		}
		for(int i=0;i<_list.size();i++) {
			MaskImageView iv = new MaskImageView(mContext);
			iv.setFocusable(false);
			iv.setScaleType(ScaleType.CENTER_CROP);
			LayoutParams layoutparams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
			iv.setScaleType(ScaleType.CENTER_CROP);
			iv.setLayoutParams(layoutparams);
//			iv.SetRecyclableUrl
			if(_list.get(i).resorce_pic_id != 0){
				iv.setImageResource(_list.get(i).resorce_pic_id);
			}else{
				iv.SetUrl(_list.get(i).pic_url);
			}
	        iv.setTag(i);
			iv.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					if(v.getTag() != null) {
						int position = (Integer) v.getTag();
						callBackListener.onClicked(_list.get(position), position);
					}
				}
			});
			_viewList.add(iv);
		}
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return _list.size();
	}

	@Override
	public boolean isViewFromObject(View view, Object obj) {
		return view == (View) obj;
	}

	@Override
	public Object instantiateItem(ViewGroup container, final int position) {
		container.addView(this._viewList.get(position));
		return this._viewList.get(position);
		
	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		container.removeView((View) object);
	}
}
